//
//  HomePageViewController.m
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/7.
//  Copyright © 2016年 唐先才. All rights reserved.
//

#import "HomePageViewController.h"
#import "TxcPrefixHeader.pch"
#import "RecommendViewController.h"
#import "BuyViewController.h"

@interface HomePageViewController()
{
    UIButton *_button;
    UIView *_barView;
    UIViewController * _willVC;
    UIPageViewController *_pvc;
    NSArray *_viewControllersArray;
}
@end

@implementation HomePageViewController

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createNav];
    [self createPageViewController];
}

- (void)createPageViewController {
    _pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    _pvc.view.frame = _pvc.view.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    _pvc.view.backgroundColor = [UIColor whiteColor];
    _pvc.dataSource = self;
    _pvc.delegate = self;
    [self.view addSubview:_pvc.view];
    
    RecommendViewController *rvc = [[RecommendViewController alloc] init];
    rvc.viewController = self;
    BuyViewController *bvc = [[BuyViewController alloc] init];
    _viewControllersArray = @[rvc, bvc];
    [_pvc setViewControllers:@[rvc] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}

- (void)createNav {
    UIView *view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.userInteractionEnabled = YES;
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:view.frame];
    imgView.image = [UIImage imageNamed: @"main_titleBar"];
    imgView.userInteractionEnabled = YES;
    [view addSubview:imgView];
    
    NSArray *titleArray = @[@"推荐", @"海淘"];
    for (NSInteger i = 0; i < 2; i ++) {
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(i * SCREEN_WIDTH / 2, 20, SCREEN_WIDTH / 2, 36);
        _button.titleLabel.font = [UIFont systemFontOfSize:18];
        _button.titleLabel.textAlignment = NSTextAlignmentCenter;
        _button.tag = 100 + i ;
        [_button setTitle:titleArray[i] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [imgView addSubview:_button];
    }
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    _button=button;
    
    _barView = [[UIView alloc] init];
    _barView.frame = CGRectMake(SCREEN_WIDTH / 6, 58, SCREEN_WIDTH/ 6, 2);
    _barView.backgroundColor = [UIColor whiteColor];
    [imgView addSubview:_barView];
}

- (void)clickAction:(UIButton *)button {
    if (button.tag == 101) {
        [UIView animateWithDuration:0.3 animations:^{
            _barView.frame = CGRectMake(SCREEN_WIDTH * 2 / 3, 58, SCREEN_WIDTH / 6, 2);
        }];
        [_pvc setViewControllers:@[_viewControllersArray[1]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
        
    } else if (button.tag == 100) {
        [UIView animateWithDuration:0.3 animations:^{
            _barView.frame = CGRectMake(SCREEN_WIDTH / 6, 58, SCREEN_WIDTH / 6, 2);
        }];
        [_pvc setViewControllers:@[_viewControllersArray[0]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    }
    _button = button;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[BuyViewController class]]) {
        return nil;
    }
    return _viewControllersArray[1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[RecommendViewController class]]) {
        return nil;
    }
    return _viewControllersArray[0];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed) {
        if ([_willVC isKindOfClass:[BuyViewController class]]) {
            [UIView animateWithDuration:0.3 animations:^{
                _barView.frame = CGRectMake(2 * SCREEN_WIDTH / 3, 58, SCREEN_WIDTH / 6, 2);
            }];
            UIButton *button = (UIButton *)[self.view viewWithTag:101];
            _button=button;
        } else {
            [UIView animateWithDuration:0.3 animations:^{
                _barView.frame = CGRectMake(SCREEN_WIDTH / 6, 58, SCREEN_WIDTH / 6, 2);
            }];
            UIButton *button = (UIButton *)[self.view viewWithTag:100];
            _button=button;
        }
    }
}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
    _willVC = pendingViewControllers[0];
}

@end
